#include "qelib.h"
#include "libfdt.h"


QELOG_DOMAIN("arm-gic");



typedef struct
{
    qe_intc_dev intc;
} arm_gic_dev;

static qe_ret arm_gic_irq_register(qe_intc_dev *dev, qe_int irq, qe_irq_handler handler, void *args)
{
    qe_debug("register irq:%d", irq);
    return qe_irq_register(irq, handler, args);
}

static void arm_gic_irq_unregister(qe_intc_dev *dev, int irq)
{
    return qe_irq_unregister(irq);
}

static qe_ret arm_gic_irq_enable(qe_intc_dev *dev, qe_int irq)
{
    qe_debug("enable irq:%d", irq);
    return qe_irq_enable(irq);
}

static void arm_gic_irq_disable(qe_intc_dev *dev, int irq)
{
    return qe_irq_disable(irq);
}

static qe_intc_ops arm_gic_ops = {
    .irq_register = arm_gic_irq_register,
    .irq_unregister = arm_gic_irq_unregister,
    .irq_enable = arm_gic_irq_enable,
    .irq_disable = arm_gic_irq_disable,
};

static qe_ret arm_gic_probe(const void *fdt, int offset)
{
    const char *name;
    arm_gic_dev *dev;

    name = fdt_get_name(fdt, offset, QE_NULL);

    dev = qe_malloc(sizeof(arm_gic_dev));
    qe_assert(dev);

    qe_debug("register %s ", name);

    qe_intc_register(&dev->intc, name, &arm_gic_ops);

    return qe_ok;
}

static const qe_device_id arm_gic_ids[] = {
    {.compatible="arm,cortex-a9-gic"},
    {},
};

QE_DRIVER(arm_gic) = {
    .name = "arm-gic",
    .of_match = arm_gic_ids,
    .probe = arm_gic_probe,
};
